package com.gitee.wsl.func.procedure.sample

import com.gitee.wsl.func.predicate.Predicate
import com.gitee.wsl.func.procedure.Procedure
import kotlin.jvm.JvmOverloads


/**
 * IfProcedure allows developers to evaluate the specified procedure only when either predicate returns true.
 * If the result of evaluating the predicate is false, and the developer has specified that there
 * is an elseProcedure, then the elseProcedure is evaluated.
 */
class IfProcedure<T> @JvmOverloads constructor(
    private val predicate: Predicate<in T>,
    private val procedure: Procedure<in T>,
    private val elseProcedure: Procedure<in T>? = null,
) : Procedure<T> {

    override fun value(each: T) {
        if (this.predicate.accept(each)) {
            this.procedure.value(each)
        } else {
            this.elseProcedure?.value(each)
        }
    }

    override fun toString(): String {
        return "new IfProcedure(" + this.predicate + ", " + this.procedure + ", " + this.elseProcedure + ')'
    }

}
